//
//  MoveTableViewController.m
//  11210
//
//  Created by student on 16/3/4.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "MoveTableViewController.h"

@interface MoveTableViewController ()

@property(nonatomic,strong)NSMutableArray *list;

@end

@implementation MoveTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.list=[@[@"Eeny", @"Meeny", @"Miney", @"Moe", @"Catch", @"A",
                @"Tiger", @"By", @"The", @"Toe"] mutableCopy];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    UIBarButtonItem *btn=[[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(btnPressed)];
    self.navigationItem.rightBarButtonItem=btn;
    
}

-(void)btnPressed{
    
    [self.tableView setEditing:!self.tableView.editing animated:YES];
    [self.navigationItem.rightBarButtonItem setTitle:self.tableView.editing?@"完成":@"编辑"];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.list.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.textLabel.text=self.list[indexPath.row];

    
    return cell;
}

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleNone;
}

-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
    NSString *obj=self.list[sourceIndexPath.row];
    [self.list removeObject:obj];
    [self.list insertObject:obj atIndex:destinationIndexPath.row];

}

@end
